package com.zzyl.serve.service.impl;

import java.util.List;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.zzyl.serve.dao.NursingPlanDto;
import com.zzyl.serve.domain.NursingProjectPlan;
import com.zzyl.serve.service.INursingProjectPlanService;
import com.zzyl.serve.vo.NursingPlanVo;
import com.zzyl.serve.vo.NursingProjectPlanVo;
import jdk.jfr.Threshold;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.zzyl.serve.mapper.NursingPlanMapper;
import com.zzyl.serve.domain.NursingPlan;
import com.zzyl.serve.service.INursingPlanService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.stream.Collectors;

/**
 * 护理计划Service业务层处理
 *
 * @author ruoyi
 * @date 2024-10-04
 */
@Service
public class NursingPlanServiceImpl extends ServiceImpl<NursingPlanMapper, NursingPlan> implements INursingPlanService {
    @Autowired
    private NursingPlanMapper nursingPlanMapper;

    @Autowired
    private INursingProjectPlanService nursingProjectPlanService;

    /**
     * 查询护理计划
     *
     * @param id 护理计划主键
     * @return 护理计划
     */
    @Override
    public NursingPlanVo selectNursingPlanById(Long id) {
        NursingPlan nursingPlan = this.getById(id);
        List<NursingProjectPlan> nursingProjectPlans = nursingProjectPlanService.list(Wrappers.
                <NursingProjectPlan>lambdaQuery().
                eq(NursingProjectPlan::getPlanId, id));


        NursingPlanVo vo = BeanUtil.toBean(nursingPlan, NursingPlanVo.class);
        List<NursingProjectPlanVo> nursingProjectPlanVos = nursingProjectPlans.stream().map(projectPlan -> {
            NursingProjectPlanVo nursingProjectPlanVo = BeanUtil.toBean(projectPlan, NursingProjectPlanVo.class);
            nursingProjectPlanVo.setProjectId(projectPlan.getProjectId().toString());
            nursingProjectPlanVo.setExecuteCycle(projectPlan.getExecuteCycle().toString());
            return nursingProjectPlanVo;
        }).collect(Collectors.toList());
        vo.setProjectPlans(nursingProjectPlanVos);
        return vo;
    }

    /**
     * 查询护理计划列表
     *
     * @param nursingPlan 护理计划
     * @return 护理计划
     */
    @Override
    public List<NursingPlan> selectNursingPlanList(NursingPlan nursingPlan) {
        return nursingPlanMapper.selectNursingPlanList(nursingPlan);
    }

    /**
     * 新增护理计划
     *
     * @param dto 护理计划
     * @return 结果
     */
    @Override
    @Transactional
    public int insertNursingPlan(NursingPlanDto dto) {
        //新增护理计划
        NursingPlan nursingPlan = BeanUtil.copyProperties(dto, NursingPlan.class);
        this.save(nursingPlan);
        //新增中间表
        List<NursingProjectPlan> projectPlans = dto.getProjectPlans();
        projectPlans.forEach(projectPlan -> {
            projectPlan.setPlanId(Long.valueOf(nursingPlan.getId()));
        });

        boolean flag = nursingProjectPlanService.saveBatch(projectPlans);
        return flag ? 1 : 0;
    }

    /**
     * 修改护理计划
     *
     * @param dto 护理计划
     * @return 结果
     */
    @Override
    @Transactional
    public int updateNursingPlan(NursingPlanDto dto) {
        NursingPlan nursingPlan = BeanUtil.copyProperties(dto, NursingPlan.class);

        List<NursingProjectPlan> projectPlans = dto.getProjectPlans();
        if (CollUtil.isNotEmpty(projectPlans)) {
            nursingProjectPlanService.remove(Wrappers.<NursingProjectPlan>lambdaQuery().eq(NursingProjectPlan::getPlanId, nursingPlan.getId()));

            projectPlans.forEach(projectPlan -> {
                projectPlan.setPlanId(nursingPlan.getId());
            });
            nursingProjectPlanService.saveBatch(projectPlans);
        }
        boolean flag = this.updateById(nursingPlan);

        return flag ? 1 : 0;
    }



    /**
     * 删除护理计划信息
     *
     * @param id 护理计划主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteNursingPlanById(Long id) {

        nursingProjectPlanService.remove((Wrappers.<NursingProjectPlan>lambdaQuery().eq(NursingProjectPlan::getPlanId, id)));
        boolean flag = this.removeById(id);
        return flag ? 1 : 0;
    }
}
